package xyz.xtt.asset.mq;

import static xyz.xtt.common.asset.AssertUtils.isNull;

import java.nio.charset.StandardCharsets;

import org.apache.commons.lang3.StringUtils;
import org.apache.rocketmq.common.message.MessageExt;
import org.apache.rocketmq.spring.annotation.RocketMQMessageListener;
import org.apache.rocketmq.spring.core.RocketMQListener;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.stereotype.Component;

import cn.hutool.json.JSONUtil;
import lombok.extern.slf4j.Slf4j;
import xyz.xtt.asset.constants.ErrorMessage;
import xyz.xtt.asset.service.IBlindBoxService;
import xyz.xtt.asset.service.IInnerService;
import xyz.xtt.asset.service.ITblAssetService;
import xyz.xtt.pay.api.dto.BusinessOrderStatusChangeMsg;
import xyz.xtt.pay.client.response.BusinessOrderDto;
import xyz.xtt.pay.client.response.BusinessOrderResponse;
import xyz.xtt.pay.client.response.TblPayOrderDto;

/**
 * @author dale
 * @date 2025/3/19
 **/
@RefreshScope
@Slf4j
@Component
@RocketMQMessageListener(topic = "${rocketmq.business.order.status.change.topic:nft_pay_business_order_status_change_topic}", selectorExpression = "asset_with_physical_pay_success", consumerGroup = "${rocketmq.consumer.group.asset.physical.asset.pay.success:nft_asset_physical_asset_pay_success_group}")
public class PhysicalNftBusinessOrderPaySuccessConsumer implements RocketMQListener<MessageExt> {
	@Autowired
	private IBlindBoxService iBlindBoxService;
	@Autowired
	ITblAssetService iTblAssetService;
	@Autowired
	private IInnerService iInnerService;

	@Override
	public void onMessage(MessageExt msg) {
		String msgId = msg.getMsgId();
		String tags = msg.getTags();
		byte[] body = msg.getBody();
		String message = new String(body, StandardCharsets.UTF_8);
		log.info("NftPurchaseBusinessOrderPaySuccessConsumer msgId:{} tags:{} message: {}", msgId, tags, message);
		if (StringUtils.isEmpty(message)) {
			return;
		}
		if (!JSONUtil.isTypeJSON(message)) {
			log.warn("NftPurchaseBusinessOrderPaySuccessConsumer Received message: {} is not json", message);
			return;
		}

		BusinessOrderStatusChangeMsg statusChangeMsg = JSONUtil.toBean(message, BusinessOrderStatusChangeMsg.class);
		isNull(statusChangeMsg, ErrorMessage.ARGUMENT_NOT_VALID);
		log.info("NftPurchaseBusinessOrderPaySuccessConsumer statusChangeMsg: {} tag:{}", message, tags);
		BusinessOrderResponse businessOrder = iInnerService.businessOrderInfoWithId(statusChangeMsg.getBusinessOrderId());
		if (businessOrder == null) {
			log.error("NftPurchaseBusinessOrderPaySuccessConsumer can not find businessOrder by id:{}", statusChangeMsg.getBusinessOrderId());
			return;
		}
		BusinessOrderDto payOrder = iInnerService.getBusinessOrderById(businessOrder.getOrderId());
		TblPayOrderDto payOrderDto = new TblPayOrderDto();
		BeanUtils.copyProperties(payOrder, payOrderDto);
		boolean openBox = iBlindBoxService.openBox(payOrderDto);
		// pk实时更新热度值-wq
		// updateHotness(payOrderDto);
	}

	/**
	 * 实时更新热度值 String goodsId, String amount
	 */
//	public void updateHotness(TblPayOrderDto payOrderDto) {
//		try {
//			PayAttach payAttach = JSONUtil.toBean(payOrderDto.getBusinessAttach(), PayAttach.class);
//			String goodsId = payAttach.getGoodsId();
//			String amount = payOrderDto.getOrderMoney().toString();
//			// 判断goodsId是不是属于亚姐
//			TblAsset tblAsset = iTblAssetService.assetInfoWithGoodsId(goodsId);
//			if (tblAsset == null || tblAsset.getFirstType() != 60) {
//				return;
//			}
//			// 查询发起者该资产现存正在pk的记录
//			List<TblMissAsiaPk> challengerList = tblMissAsiaPkMapper.findChallengerList(goodsId);
//			for (TblMissAsiaPk entity : challengerList) {
//				int sumAmount = Integer.parseInt(entity.getChallengerHotness()) + Integer.parseInt(amount);
//				// 更新记录的热力值
//				tblMissAsiaPkMapper.updateChallengerHotness(String.valueOf(entity.getId()), Integer.toString(sumAmount));
//			}
//			// 查询接受者该资产现存正在pk的记录
//			List<TblMissAsiaPk> acceptorList = tblMissAsiaPkMapper.findAcceptorList(goodsId);
//			for (TblMissAsiaPk entity : acceptorList) {
//				int sumAmount = Integer.parseInt(entity.getAcceptorHotness()) + Integer.parseInt(amount);
//				// 更新记录的热力值
//				tblMissAsiaPkMapper.updateAcceptorHotness(String.valueOf(entity.getId()), Integer.toString(sumAmount));
//			}
//		} catch (Exception e) {
//			log.info("updateHotness ex:{}", e.getMessage(), e);
//		}
//	}
}
